<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增日报')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-daliy-add">
            <div class="form-group">
                <label class="col-sm-3 control-label">日报日期：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="daliyDate" class="form-control" placeholder="yyyy-MM-dd" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div id="taskContent">
                <span style="float:right;position:relative; width:80px;height:20px;" class="bs-glyphicons glyphicon glyphicon-plus" id="addTask">
                    <span onclick="addTask()"><a>添加任务</a></span>
                </span>
            </div>

        </form>
    </div>

    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: select2-js" />
    <script th:inline="javascript">
        var num=0
        function selectProduct(sel){
            var str = sel.name;
            let index = str.indexOf(".")
            str =str .substring(0,index);
            str += ".productId";
            var productType= sel.value;
            var url = "/system/product/"+ productType;
            if ("" === productType) {
                $("select[name='"+str+"']").empty();
                $("select[name='"+str+"']").append
                ('<option value="">请选择产品/项目</option>');
                $("select[name='"+str+"']").selectpicker('refresh');
                $("select[name='"+str+"']").selectpicker('render');
                return;
            }
            $.ajax({
                cache: true,
                type: "GET",
                url: url,
                async: false,
                success: function(result) {
                    data = result.data
                    if (result.code == web_status.SUCCESS) {
                        $("select[name='"+str+"']").empty();
                        for(var i in data){
                            // json = eval("("+data[i]+")");
                            var productId = data[i].productId;
                            var productName =data[i].productName;
                            $("select[name='"+str+"']").append
                            ('<option value="'+productId+'">'+productName+'</option>');
                        };
                        $("select[name='"+str+"']").selectpicker('refresh');
                        $("select[name='"+str+"']").selectpicker('render');
                    } else {
                        $.modal.alertError(result.msg);
                    }
                }
            });
        };
        var prefix = ctx + "system/daliy"
        function addTask() {
            var url;
            url = prefix + "/addTask/"+num;
            $.ajax({
                cache: true,
                type: "GET",
                url: url,
                async: false,
                success: function(result) {
                    $("#addTask").after(result);
                    num++;
                }
            });
        };
        function delTask(num) {
            var dev = "#div_" + num;
            $(dev).remove();
        };
        $("#form-daliy-add").validate({
            focusCleanup: true
        });

        function submitHandler() {

            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-daliy-add').serialize());
            }
            // $('#form-daliy-add').ajaxSubmit({
            //     type: 'post', // 提交方式 get/post
            //     url: prefix + "/add", // 需要提交的 url
            //     data: $('#form-daliy-add').serialize(),
            //     success: function(data) { // data 保存提交后返回的数据，一般为 json 数据
            //         // 此处可对 data 作相关处理
            //         alert('提交成功！');
            //     }
            // });
        }



        $(function() {
            $("input[name='daliyDate']").datetimepicker({
                format: "yyyy-mm-dd",
                minView: "month",
                autoclose: true
            });
            var nowTime=new Date();
            $("input[name='daliyDate']").val(nowTime.format("yyyy-MM-dd"));
            addTask(num);
        });
    </script>
</body>
</html>